/******************************************************************
 *
 *$RCSfile: EntityDescriptorDropDownListener.java,v $ $Revision: 1.4 $ $Author: @author simplefan $ $Date: 2009/07/21 09:37:44 $
 *
 *$Log: EntityDescriptorDropDownListener.java,v $
 *Revision 1.4  2009/07/21 09:37:44  @author simplefan
 **** empty log message ***
 *
 *Revision 1.3  2009/07/07 12:58:44  @author simplefan
 **** empty log message ***
 *
 *Revision 1.2  2009/02/27 09:39:03  shenwq
 **** empty log message ***
 *
 *Revision 1.1  2009/01/15 05:37:23  shenwq
 **** empty log message ***
 *
 *
 ********************************************************************/
package com.simplefan.meta.determine.client.web.listener;

import java.util.ArrayList;
import java.util.List;

import com.bstek.dorado.common.DoradoContext;
import com.bstek.dorado.data.Dataset;
import com.simplefan.meta.determine.client.desktop.service.MetaPlatformServiceFacade;
import com.simplefan.meta.determine.common.po.EntityDescriptor;
import com.simplefan.meta.determine.common.po.EntitySpec;

/**
 * EntityDescriptorDropDownListener
 */
public class EntityDescriptorDropDownListener extends MetaPlatformListener {
	@Override
  public void afterLoadData(Dataset dataset) throws Exception {

		super.afterLoadData(dataset);
		String entitySpecId = DoradoContext.getContext().getParameter(
				"entitySpecId");
		if(entitySpecId == null || "".equals(entitySpecId)){
			entitySpecId = dataset.parameters().getString("entitySpecId");
		}
		if (entitySpecId != null && !entitySpecId.equals("")) {
			MetaPlatformServiceFacade facade = getServiceFacade();
			List<EntityDescriptor> list = new ArrayList<EntityDescriptor>();
			EntitySpec entitySpec = facade.findEntitySpecById(Long.parseLong(entitySpecId));
			while(entitySpec!=null){
				List<EntityDescriptor> temp = facade.queryEntityDescriptor
						(entitySpec.getId());
				if(temp!=null && temp.size()>0){
					list.addAll(temp);
				}
				entitySpec = entitySpec.getParentEntitySpec();
			}
			
			dataset.fromDO(list);
		}
		
		String className =  DoradoContext.getContext().getParameter(
				"className");
		if(className == null || "".equals(className)){
			className = dataset.parameters().getString("className");
		}
		if (className != null && !className.equals("")) {
			MetaPlatformServiceFacade facade = getServiceFacade();
//			EntitySpec entitySpec = facade.getServiceFactory().getMetaPlatformService()
//			.findEntitySpecByClassName(className);
//			List<EntityDescriptor> list = facade
//					.queryEntityDescriptorWithParent(entitySpec.getId());
			
			List<EntityDescriptor> list = new ArrayList<EntityDescriptor>();
			EntitySpec entitySpec = facade.getServiceFactory().getMetaPlatformService()
			.findEntitySpecByClassName(className);
			while(entitySpec!=null){
				List<EntityDescriptor> temp = facade.queryEntityDescriptor
						(entitySpec.getId());
				if(temp!=null && temp.size()>0){
					list.addAll(temp);
				}
				entitySpec = entitySpec.getParentEntitySpec();
			}
			dataset.fromDO(list);
		}
	}

}
